

// findFocusImgList();//首页轮播图加载函数
// reCategoryList();//餐厅分类列表
// findAllRestaurant(0);//查询所有餐厅

//首页轮播图加载函数
function findFocusImgList(){
	var page = 0;
	var size = 5;
	
	var sort = new Array();
	sort.push("id");
	
	var obj = JSON.stringify({
		
	});
    httpHelperpost(_API_FIND_FOCUS_IMG_LIST+"?page="+page+"&size="+size+"&sort="+sort, obj, function(isSuccess, data) {
        if (isSuccess == true) {
            var response = data.responseText;
            var value = JSON.parse(response);
    			
   			// console.log(response);
   			var shuffHtml = "";
   			
   			for(var i=0;i<value.data.content.length;i++){
   				shuffHtml+="<div class='swiper-slide'><img src="+value.data.content[i].image+"></div>";
   			}
   			
   			$(".swiper-wrapper").html(shuffHtml);

            if(value.code == 0){
             	console.log("查询成功");
             	//初始化轮播图插件
             	var mySwiper = new Swiper ('.swiper-container', {
				    	direction: 'horizontal',
				    	loop: true,
				    
				    	// 如果需要分页器
				    	pagination: '.swiper-pagination',
				    	paginationClickable: true,
				});
            }else if(value.code==401){
                alert(value.message);
                window.location.href='./index.html';
            }else{
                alert(value.message);
             	console.log("查询失败");
            }
        }else {}
    });
}

//餐厅分类列表
function reCategoryList(){
	var page = 0;
	var size = 8;
	
	var sort = new Array();
	sort.push("id");
	
	var obj = JSON.stringify({
		
	});
    httpHelperpost(_API_REST_CATEGORY_LIST+"?page="+page+"&size="+size+"&sort="+sort, obj, function(isSuccess, data) {
        if (isSuccess == true) {
            var response = data.responseText;
            var value = JSON.parse(response);
    			
   			// console.log(response);
   			var typeHtml ="";
   			for(var i=0;i<value.data.content.length;i++){
   				typeHtml += "<div class='type-every' onclick=window.location.href='./classOther.html?rcId="+value.data.content[i].id+"'>"
						+ "<div class='type-img'><span><img src="+value.data.content[i].icon+"></span></div>"
						+ "<div class='type-text'>"+value.data.content[i].name+"</div></div>";
   			}
   			$(".type").html(typeHtml);

            if(value.code == 0){
             	console.log("查询成功");
             	
            }else if(value.code==401){
                alert(value.message);
                window.location.href='./index.html';
            }else{
                alert(value.message);
             	console.log("查询失败");
            }
        }else {}
    });
}


//查询所有餐厅
function findAllRestaurant(isRefresh){
    
    var size = 30;
    var page = Math.ceil($(".everyDay .everyDay-li").length/size);
    
    var sort = new Array();
    sort.push("id");
    
    var obj = JSON.stringify({
    });
    httpHelperpost(_API_RESTAURANT+"?page="+page+"&size="+size+"&sort="+sort, obj, function(isSuccess, data) {
        if (isSuccess == true) {
            var response = data.responseText;
            var value = JSON.parse(response);
                
            // console.log(response);

            if(value.code == 0){
                var liHtml = "";
                var activity = new Array();
    
                var activityHtml = "";
                var supportHtml = "";
                for(var i=0;i<value.data.length;i++){
                    
                    if(value.data[i].fullReduceActivity==null||value.data[i].fullReduceActivity==""||value.data[i].fullReduceActivity==undefined){
                        activityHtml = "";
                    }else{
                        activityHtml = "<img src='./img/pre_01.png'><span class='prefer-inc'>";
                        activity = value.data[i].fullReduceActivity.split(",");
                        
                        for(var j=0;j<activity.length-1;j++){
                            activityHtml += "<span>"+activity[j]+"</span>";
                        }
                        activityHtml+="</span>";
                    }
    
                    if(value.data[i].dispatchType==null||value.data[i].dispatchType==""||value.data[i].dispatchType==undefined){
    
                    }else{
                        supportHtml = "<div class='everyDay-support'>";
                        for(var k=0,len = value.data[i].dispatchType.split(":").length;k<len;k++){
                            if(value.data[i].dispatchType.split(":")[k]==1){
                                supportHtml+="支持食堂&nbsp&nbsp";
                            }else if(value.data[i].dispatchType.split(":")[k]==2){
                                supportHtml+="支持外卖&nbsp&nbsp";
                            }else if(value.data[i].dispatchType.split(":")[k]==3){
                                supportHtml+="支持自取&nbsp&nbsp";
                            }
                        }
                    }
                    
                    
                    liHtml += "<li class='everyDay-li' onclick=window.location.href='./detail.html?id="+value.data[i].id+"'>"
                            +"<div class='everyDay-img'><img src="+value.data[i].icon+"></div>"
                            +"<div class='everyDay-bar'>"
                            +"<div>"
                            +"<span class='everyDay-title'>"+textisNull(value.data[i].name)+"</span>"
                            // +"<img class='everyDay-more' src='./img/dot_01.png'>"
                            +"</div>"
                            +"<div class='everyDay-star'>";
                                for(var j=0,len=Math.round(levelisNull(value.data[i].evaluationLevel));j<len;j++){
                                    liHtml+="<img src='./img/start.png'>";
                                }
                    liHtml +="</div>"
                            +"<div class='everyDay-detail'>"
                            +"<div>起送￥<span>"+value.data[i].limitPrice+"</span></div>"
                            +"<div>配送费<span class='shipFee'>"+value.data[i].dispatchPrice+"</span>元</div>"
                            // +"<div><span class='everyDay-min'>53分钟</span><span class='everyDay-shu'>|</span><span>1.6km</span></div>"
                            +"</div>"
                            +"<div></div>"
                            +supportHtml
                            // +"<div class='everyDay-support'>支持自取</div>"
                            +"<div class='everyDay-prefer'>"
                            // +"<img src='./img/pre_01.png'>"
                                +activityHtml
                            // +"<span class='prefer-inc'>"+activityHtml+"</span>"
                            +"</div></div></li>";
                    }

                if(isRefresh==0){
                    $(".everyDay ul").html(liHtml);
                }else{
                    $(".everyDay ul").append(liHtml);
                }
                if(value.data==""){

                }else{
                    dropRefresh();  
                }
            
                
                console.log("查询成功");
            }else if(value.code==401){
                alert(value.message);
                window.location.href='./index.html';
            }else{
                alert(value.message);
                console.log("查询失败");
            }
        }else {}
    });

    function levelisNull(value){
        if(value==null||value==undefined){
            value = 0;
        }
        return value;
    }
    function textisNull(value){
        if(value==null||value==undefined){
            value = "";
        }
        return value;
    }
}


//首页搜索跳转
function indexSearch(that){
    var nameOrCname = $(that).siblings("input").val();
    if(nameOrCname==null||nameOrCname==""||nameOrCname==undefined){
        alert("请输入搜索内容");
    }else{
        window.location.href='./indexSearch.html?nameOrCname='+nameOrCname;
    }
}

//下拉刷新
function dropRefresh(){
    
    $(window).scroll(function() {
        var scrollTop = $(document).scrollTop();//滚动条距离页面顶部距离
        var offsetHeight = $(document).height();//当前页面高度(包括滚动页面)
        var clientHeight = $(this).height();//当前页面高度(不包括滚动页面)
        var bottomHeight = scrollTop-(offsetHeight-clientHeight);
        
        if(bottomHeight<2&&bottomHeight>-2){
            // console.log(bottomHeight);
            findAllRestaurant(1);
            $(window).unbind("scroll");     
        }
    });
}